package com.generalbackend.role.controller;

import com.generalbackend.common.annotion.ApiPermission;
import com.generalbackend.common.enums.MenuEnum;
import com.generalbackend.common.model.CommonResult;
import com.generalbackend.role.model.dto.RoleDTO;
import com.generalbackend.role.model.query.RoleQuery;
import com.generalbackend.role.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
* @Description 角色表控制层
* @author tim.xie
* @date 2021-12-16 10:51
*/
@RestController
@RequestMapping(value = "/api/role")
public class RoleController {

    @Autowired
    private IRoleService  roleService;

    @ApiPermission(name = "",menu = {MenuEnum.RoleManage})
    @ApiOperation(httpMethod = "POST" ,value = "角色列表")
    @RequestMapping(value = "/list" ,method = RequestMethod.POST)
    public CommonResult list(@RequestBody RoleQuery roleQuery ){
           return  CommonResult.successCommonResult(roleService.list(roleQuery),"查询列表成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.RoleManage})
    @ApiOperation(httpMethod = "POST",value = "添加角色")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public CommonResult add(@RequestBody RoleDTO roleDTO){
        roleService.insert(roleDTO);
        return CommonResult.successCommonResult("新增角色表成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.RoleManage})
    @ApiOperation(httpMethod = "POST",value = "修改角色")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public CommonResult update(@RequestBody RoleDTO roleDTO){
        roleService.update(roleDTO);
        return CommonResult.successCommonResult("修改角色表成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.RoleManage})
    @ApiOperation(httpMethod ="DELETE", value = "删除角色")
    @RequestMapping(value = "/deleteById",method = RequestMethod.DELETE)
    public CommonResult deleteById(@RequestParam("id")Long id){
        roleService.deleteById(id);
        return CommonResult.successCommonResult("删除角色表成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.RoleManage})
    @ApiOperation(httpMethod = "POST",value = "通过角色id获取角色信息")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public CommonResult getById( @RequestParam("id")Long id ){
        return CommonResult.successCommonResult(roleService.selectById(id),"查询成功");
    }

    @ApiPermission(name = "",menu = {MenuEnum.RoleManage, MenuEnum.UserManage})
    @ApiOperation(httpMethod = "POST",value = "获取所有角色")
    @RequestMapping(value = "/getAllRole",method = RequestMethod.GET)
    public CommonResult getAllRole() {
        return CommonResult.successCommonResult(roleService.getAllRole(), "查询成功");
    }

}
